// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// o [author] rhyme_lph
// S [email]  rhymelph@gmail.com
// q [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/entities/role_permission_entity.dart';
import 'package:finance_app/src/http_service/http_permission_service.dart';
import 'package:finance_app/src/router_gen/router_gen_providers.dart';
import 'package:finance_app/src/widgets/bottom_sheet/list_select_bottom_sheet.dart';

class ManageRoleAccessProvider
    extends BaseProvider<List<RolePermissionEntity>> {
  @override
  Future get dataFuture => HttpPermissionService.getAllRoles();

  List<String> menuList = ['Add Role', 'Duplicate Role'];

  void onSelectMenu(String value) {
    switch (value) {
      case 'Add Role':
        onNavRolePage(null);
        break;
      case 'Duplicate Role':
        onDuplicateRole();
        break;
    }
  }

  void onDuplicateRole() {
    obtainContext?.call((context) async {
      final result = await showRolesBottomSheet(context, data,
          title: S.current.duplicate_role);
      if (result != null) {
        onNavRolePage(result, true);
      }
    });
  }

  void onNavRolePage(RolePermissionEntity data,
      [bool isDuplicate = false]) async {
    final result =
        await Routes.navigateTo(RouterGenProviders.newRolePage, params: {
      'data': data,
      'isDuplicate': isDuplicate,
    });

    // if (result != null && result['delete']) {
    await post(HttpPermissionService.getAllRoles(), onSuccess: (result) {
      this.data = getEntityFromResult<List<RolePermissionEntity>>(result);
      update();
    });
    // }
  }

  void onSave() {}
}
